/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zx.pm.common.persistence.Page;
import com.zx.pm.common.service.CrudService;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.modules.zx.dao.LzdDao;
import com.zx.pm.modules.zx.dao.MakeTenderDao;
import com.zx.pm.modules.zx.dao.TenderFileDao;
import com.zx.pm.modules.zx.entity.Lzd;
import com.zx.pm.modules.zx.entity.MakeTender;
import com.zx.pm.modules.zx.entity.TenderFile;

/**
 * 标书制作Service
 * @author forest
 * @version 2019-02-11
 */
@Service
@Transactional(readOnly = true)
public class MakeTenderService extends CrudService<MakeTenderDao, MakeTender> {

	@Autowired
	private TenderFileDao tenderFileDao;
	
	@Autowired
	private LzdDao lzdDao;
	
	public MakeTender get(String id) {
		return super.get(id);
	}
	
	public List<MakeTender> findList(MakeTender makeTender) {
		return super.findList(makeTender);
	}
	
	public Page<Lzd> findLzdPage(Page<Lzd> page, Lzd entity) {
		entity.setPage(page);
		page.setList(dao.findLzdList(entity));
		return page;
	}
	
	
	public Page<Lzd> findAuditList(Page<Lzd> page, Lzd entity){
		entity.setPage(page);
		page.setList(dao.findAuditList(entity));
		return page;
	}
	
	public Page<MakeTender> findPage(Page<MakeTender> page, MakeTender makeTender) {
		return super.findPage(page, makeTender);
	}
	
	@Transactional(readOnly = false)
	public void save(MakeTender makeTender) {
		// 更新项目编码
		dao.updateProNo(makeTender.getProNo(), makeTender.getLzdId());
		if (makeTender.getIsNewRecord()){
			makeTender.preInsert();
			dao.insert(makeTender);
		}else{
			makeTender.preUpdate();
			dao.update(makeTender);
		}
		// 保存招标文件
		List<TenderFile> list = makeTender.getFileList();
		if(list != null && list.size() > 0) {
			for(TenderFile t : list) {
				// ID不为空进行更新
				if(StringUtils.isNotBlank(t.getId())) {
					// 如果文件url为空,视为该文件已被删除,则进行删除数据操作
					if(StringUtils.isBlank(t.getFileUrl())) {
						tenderFileDao.delete(t);
					}else {
						t.preUpdate();
						// 更新标书，将标书审核状态置为0,让该标书能够重新审核
						//t.setAuditState("0");
						tenderFileDao.update(t);
					}
				} else {
					if(StringUtils.isNotBlank(t.getFileUrl())) {
						t.preInsert();
						t.setLzdId(makeTender.getLzdId());
						t.setMakeTenderId(makeTender.getId());
						tenderFileDao.insert(t);
						// 更新项目流转状态为制作标书阶段
						lzdDao.updateLzdStaut("2", makeTender.getLzdId());
					}
				}
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(MakeTender makeTender) {
		super.delete(makeTender);
	}
	
	@Transactional(readOnly = false)
	public void updateProNo(String proNo, String lzdId) {
		dao.updateProNo(proNo, lzdId);
	}
	
	
	@Transactional(readOnly = false)
	public void tenderAudit(TenderFile tf) {
		tf.setAuditDate(new Date());
		tenderFileDao.tenderAudit(tf);
	}
	
	public List<MakeTender> getList(MakeTender mt){
		return dao.getList(mt);
	}
	
	public MakeTender getMakeTenderUser(String lzdId){
		return dao.getMakeTenderUser(lzdId);
	}
}